package yo.latyoo.hokage.tools.util.db;

import com.fasterxml.jackson.core.type.TypeReference;
import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;
import yo.latyoo.hokage.tools.util.JsonUtil;
import yo.latyoo.hokage.tools.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

@Converter(
        autoApply = true
)
public class ListStringConvert extends TypeReference<List<String>> implements AttributeConverter<List<String>, String> {

    public ListStringConvert() {}

    public String convertToDatabaseColumn(List<String> attribute) {
        return attribute != null ? JsonUtil.toJson(attribute) : null;
    }

    public List<String> convertToEntityAttribute(String dbData) {
            return !StringUtil.isNull(dbData) ?  JsonUtil.parseJson(dbData, new TypeReference<>() {}) : new ArrayList<>();
        }
}
